package com.example.healthapp.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;

import com.example.healthapp.R;
import com.example.healthapp.utils.MessageNotification;

// 通知服务
public class NotificationService extends Service {

    private static final String NOTIFICATION_ID = "777";
    Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        handler = new Handler();
//        startNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 执行您的服务逻辑
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                String[] content = {"注意休息哦", "记得喝水", "适当运动运动", "调整一下心情，今天元气满满"};
                while (i < 30) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    int finalI = i;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            MessageNotification.sendNotification(NotificationService.this, "健康e生", content[finalI % content.length]);
                        }
                    });
                    i++;
                    if (i == 28) i = 0;
                }
            }
        }).start();
        return START_STICKY; // 如果服务被系统杀死，将自动重启服务
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        // 停止服务
        stopSelf();
        super.onDestroy();
    }
}